package com.sdx.console.eol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdx.console.base.entity.BaseUniv;
import com.sdx.console.eol.entity.EolUnivLink;
import com.sdx.console.wx.HttpUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @ClassName Scheduled
 * @Description
 * @Author kangbin
 * @Date 2020/6/10 17:14
 */
@Component
@EnableScheduling
public class EolScheduled {

    @Autowired
    private SQLManager sqlManager;

    @Value("${eol.univ.scheduled:false}")
    private boolean flag;

    @Scheduled(cron="0 0 6 * * ?")
    public void update(){
        System.out.println("****************开始执行*****************");
       try {
           if(flag){
               String path = "https://static-data.eol.cn/www/2.0/elink/list.json?"+System.currentTimeMillis();
               String res = HttpUtils.httpGet(path);
               JSONObject json = JSON.parseObject(res);
               if("0000".equals(json.getString("code"))){
                   JSONArray data = json.getJSONArray("data");
                   for (Object datum : data) {
                       JSONObject object = (JSONObject) datum;
                       Integer school_id = object.getInteger("school_id");
                       String name = object.getString("name");
                       String m_elink = object.getString("m_elink");
                       String pc_elink = object.getString("pc_elink");
                       String m_detail = object.getString("m_detail");
                       String pc_detail = object.getString("pc_detail");
                       EolUnivLink eolUnivLink = new EolUnivLink();
                       eolUnivLink.setEolUnivId(school_id);
                       EolUnivLink eolUnivLink1 = sqlManager.templateOne(eolUnivLink);
                       eolUnivLink.setUnivName(name);
                       eolUnivLink.setPcDetail(pc_detail);
                       eolUnivLink.setMDetail(m_detail);
                       eolUnivLink.setPcElink(pc_elink);
                       eolUnivLink.setMElink(m_elink);
                       //判断是否存在，存在先删除然后在插入
                       if(eolUnivLink1!=null){
                           sqlManager.deleteObject(eolUnivLink1);
                           eolUnivLink.setUnivId(eolUnivLink1.getUnivId());
                           eolUnivLink.setUpdateTime(new Date());
                       }else{
                           BaseUniv baseUniv = new BaseUniv();
                           baseUniv.setUnivName(name);
                           BaseUniv univ = sqlManager.templateOne(baseUniv);
                           if(univ!=null){
                               eolUnivLink.setUnivId(univ.getUnivId());
                           }
                       }
                       sqlManager.insert(eolUnivLink);
                   }
               }

               sqlManager.executeUpdate(new SQLReady("update sdxdb.base_univ set pc_consult = 0,m_consult=0"));
               sqlManager.executeUpdate(new SQLReady("update sdxdb.base_univ set m_consult = 1 where univ_id in (select univ_id from eol_univ_link where univ_id is not null and m_elink!='')"));
               sqlManager.executeUpdate(new SQLReady("update sdxdb.base_univ set pc_consult = 1 where univ_id in (select univ_id from eol_univ_link where univ_id is not null and pc_elink!='')"));


           }

       } catch (IOException e) {
           e.printStackTrace();
       }


    }
}
